package com.hd.common.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.Calendar;
import java.util.Random;
import java.util.UUID;

/**
 * @author chentl
 * @version V1.0
 * @Project 交通运输第三方监测平台
 * @Title IdGenUtil.java
 * @Description id生成工具类
 * @Package com.hd.common.utils
 * @date 2020/12/31 3:48 下午
 * @Copyright: 深圳市宏电技术股份有限公司 All rights reserved.
 */
public final class GenUtil {

    /**
     * @return java.lang.String
     * @author chentl
     * @description 生成业务id
     * @date 2020/12/31 3:48 下午
     * @params []
     */
    public static String newId() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 生成运政系统秘钥
     * @date 2021/1/13 1:53 下午
     * @params [userCode]
     */
    public static String getECode(String userCode) {
        Calendar cal = Calendar.getInstance();

        int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
        int nowHour = cal.get(Calendar.HOUR_OF_DAY);
        int nowMinute = cal.get(Calendar.MINUTE);

        int tick = ((dayOfYear - 1) * 288 + (nowHour - 1) * 12 + (nowMinute / 5));
        String cipherOrigin = userCode + tick;

        String cipherText = DigestUtils.md5Hex(cipherOrigin.getBytes()).toUpperCase();
        Random r1 = new Random();
        int i = r1.nextInt(cipherText.length() - 6);
        return cipherText.substring(i, i + 6).toUpperCase();
    }

    /**
     * @return java.lang.String
     * @author chentl
     * @description 快速获取缓存key
     * @date 2021/1/28 11:23 上午
     * @params [prefix, suffix]
     */
    public static String cacheKey(String prefix, String... suffix) {
        StringBuilder prefixBuilder = new StringBuilder(prefix);
        for (String s : suffix) {
            prefixBuilder.append(s);
        }
        return prefixBuilder.toString();
    }

    public static void main(String[] args) {
        String et = getECode("HJ_DSJPT");
        System.out.println(et);

    }


}
